'APPENDIX-A
'EViews Programming Code for Korea

wfopen  "C:\Users\Maryam\Desktop\BS Studies\PhD Thesis-II\EViews and STATA Progarm Codes\Chapter-6\korea.wf1"

'****************************************************************************
'Group Plot for RER_CPI, RER_DEF, RER_DEF_NT and A_TILDE
'****************************************************************************
group gA rer_cpi rer_def rer_def_nt A_tilde
freeze(group_plot) gA.line(x)
group_plot.setelem(1) lcolor(black) symbol(7) lpat(1)
group_plot.setelem(2) lcolor(black) symbol(4) lpat(1)
group_plot.setelem(3) lcolor(black) symbol(1) lpat(1)
group_plot.setelem(3) lcolor(black)
group_plot.options linepat
group_plot.addtext(t) Real ERs and Productivity Gap (Korea & U.S): 1970-2013
group_plot.addtext(b) Year
group_plot.addtext(l) RER_CPI
group_plot.addtext(l) RER_DEF
group_plot.addtext(l) RER_CPI, RER_DEF & RER_DEF_NT
group_plot.addtext(r) A_TILDE
'************************************************************
'************************************************************
create y 1970 2013
'importing data from Excel for Korea
import  "C:\Users\Maryam\Desktop\BS Studies\PhD Thesis-II\EViews and STATA Progarm Codes\Chapter-6\Chapter 6.xlsx" range="Korea"

'***************************************************************************************************
'CASE-1: ESTIMATING BALASSA-SAMUELSON EFFECT FOR RER_CPI & A_TILDE
'***************************************************************************************************
'*************************************************************
'STEP 0: Tests for Unit Root in Individual Time Series
'*************************************************************
'*********************************
'Graph for Korea's RER_CPI
'*********************************
                                        
genr rer_cpi = rer_cpi
freeze(figure_rer_cpi) rer_cpi.line
figure_rer_cpi.addtext(t) rer_cpi (Korea):  1970-2013
figure_rer_cpi.addtext(b) Year
figure_rer_cpi.addtext(l) rer_cpi
figure_rer_cpi.legend(off)
                                                 
'We see from the FIGURE that rer_cpi has time trend to it.  So we would include both an intercept and a time trend in our unit root regression equations. 

'************************************************
'ADF Unit Root Test for Korea's RER_CPI
'************************************************
 
freeze(table_6_3_rer_cpi_adf) rer_cpi.uroot(adf,trend,info=sic)

'Note that the SIC automatic lag selection picks lags, p = 1.  The unit root test produces a t-value of -3.32 which is greater than our 5% criterion -3.52.  Thus, at this point, we cannot reject the null of a unit root.

'Now, let's check for white noise.  To do that, I first set all the residuals = 0, then run the ADF test and finally will check for white noise.

genr resid = 0
freeze(mode=overwrite,rer_cpi_adf) rer_cpi.uroot(adf,const,trend,info=sic)
freeze(rer_cpi_adf_correl) resid.correl
 
'Based on the Q-statistic, I conclude that the residuals are white noise.  Putting it all together, I conclude that the rer_cpi series is not level stationary.

'The next thing I do is test whether the differenced series is stationary using the ADF test.  I once again begin by graphing the (differenced) series.
 
genr rer_cpidiff = d(rer_cpi)
freeze(figure_rer_cpidiff) rer_cpidiff.line
figure_rer_cpidiff.addtext(t) Drer_cpi (Korea):  1970-2013
figure_rer_cpidiff.addtext(b) Year
figure_rer_cpidiff.addtext(l) drer_cpi
figure_rer_cpidiff.legend(off)

'From the graph, the series clearly does not have a time trend to it. So, I would test the series for unit with an intercept only.

'So we begin the whole process over again: 

genr rer_cpidiff = d(rer_cpi)
freeze(table_6_3_rer_cpidiff1_adf) rer_cpidiff.uroot(adf,const,info=sic)

'Note that the SIC automatic lag selection picks no lags, p =0.  The unit root test produces a t-value of -5.42 which is now smaller than our 5% criterion -2.93.  Thus, we may now reject the null of non-stationarity in first differenced series of rer_cpi.  There is no reason to go further.  The last thing we do is to check ADF regression result for white noise.

genr resid = 0
freeze(mode=overwrite,rer_cpidiff1_adf) rer_cpidiff.uroot(adf,const,info=sic)
freeze(rer_cpidiff1_adf_correl) resid.correl

''Based on the Q-statistic, I conclude that the residuals are white noise.  Putting it all together, I conclude that the rer_cpi series is I(1).

'*****************************************************
'DF-GLS Unit Root Test for Korea's RER_CPI
'*****************************************************
 
freeze(table_6_3_rer_cpi_dfgls) rer_cpi.uroot(dfgls,trend,info=sic)

'Note that the SIC automatic lag selection picks lags, p = 1.  The unit root test produces a t-value of -3.39 which is smaller than our 5% criterion -3.19.  Thus, at this point, we may reject the null of unit root.
 
''Putting it all together, I conclude that the rer_cpi series is I(0), a finding incompatible with my ADF test results.

'**********************************************
'Graph for Korea's Productivity (a_tilde)
'**********************************************
                                        
genr a_tilde = a_tilde
freeze(figurea_tilde) a_tilde.line
figurea_tilde.addtext(t) a_tilde (Korea):  1970-2013
figurea_tilde.addtext(b) Year
figurea_tilde.addtext(l) a_tilde
figurea_tilde.legend(off)
                                                 
'We see from the FIGURE that a_tilde has time trend to it.  So we would include both an intercept and a time trend in our unit root regression equations.  

'**************************************************
'ADF Unit Root Test for Korea's Productivity
'**************************************************
 
freeze(table_6_3_a_tilde_adf) a_tilde.uroot(adf,trend,info=sic)

'Note that the SIC automatic lag selection picks lags, p = 0.  The unit root test produces a t-value of -1.5 which is greater than our 5% criterion -3.52.  Thus, at this point, we cannot reject the null of a unit root.

'Now, let's check for white noise.  To do that, I first set all the residuals = 0, then run the ADF test and finally will check for white noise.

genr resid = 0
freeze(mode=overwrite,a_tilde_adf) a_tilde.uroot(adf,const,trend,info=sic)
freeze(a_tilde_adf_correl) resid.correl
 
'Based on the Q-statistic, I conclude that the residuals are white noise.  Putting it all together, I conclude that the a_tilde series is not level stationary.

'The next thing I do is test whether the differenced series is stationary using the ADF test.  I once again begin by graphing the (differenced) series.
 
genr a_tildediff = d(a_tilde)
freeze(figure_a_tildediff) a_tildediff.line
figure_a_tildediff.addtext(t) da_tilde (Korea):  1970-2013
figure_a_tildediff.addtext(b) Year
figure_a_tildediff.addtext(l) da_tilde
figure_a_tildediff.legend(off)

'From the graph, the series clearly does not have a time trend to it. So, I would test the series for unit with an intercept only.

'So we begin the whole process over again: 

genr a_tildediff = d(a_tilde)
freeze(table_6_3_a_tildediff1_adf) a_tildediff.uroot(adf,const,info=sic)

'Note that the SIC automatic lag selection picks no lags, p =0.  The unit root test produces a t-value of -5.81 which is now smaller than our 5% criterion -2.93.  Thus, we may now reject the null of non-stationarity in first differenced series of a_tilde.  There is no reason to go further.  The last thing we do is to check ADF regression result for white noise.

genr resid = 0
freeze(mode=overwrite,a_tildediff1_adf) a_tildediff.uroot(adf,const,info=sic)
freeze(a_tildediff1_adf_correl) resid.correl

''Based on the Q-statistic, I conclude that the residuals are white noise.  Putting it all together, I conclude that the a_tilde series is I(1).

'*******************************************************
'DF-GLS Unit Root Test for Korea's Productivity
'*******************************************************
 
freeze(table_6_3_a_tilde_dfgls) a_tilde.uroot(dfgls,trend,info=sic)

'Note that the SIC automatic lag selection picks lags, p = 0.  The unit root test produces a t-value of -1.15 which is greater than our 5% criterion -3.19.  Thus, at this point, we may not reject the null of a unit root.
 
'Now let's see if the series is difference stationary or not

genr a_tildediff = d(a_tilde)
freeze(table_6_3_a_tildediff1_dfgls) a_tildediff.uroot(dfgls,const,info=sic)

'Note that the SIC automatic lag selection picks no lags, p = 0.  The unit root test produces a t-value of -5.88 which is now smaller than our 5% criterion -1.94. Thus, we may reject the null of non-stationarity in first differenced series of a_tilde.  

''Putting it all together, I conclude that the a_tilde series is I(1), a finding compatible with my ADF test results.

'*********************************************
'Single Equation Cointegration Methods
'*********************************************
'**********************************************************
''Graph the suspected cointegrated series together
'**********************************************************

'The first step is to plot a graph of the suspected series.  This is very important!

group g1 rer_cpi a_tilde
freeze(figure6_3a) g1.line(x)
figure6_3a.setelem(1) lcolor(black) 
figure6_3a.setelem(2) lcolor(black) lpat(8)
figure6_3a.options linepat
figure6_3a.addtext(t) rer_cpi and a_tilde (Korea & U.S): 1970-2013
figure6_3a.addtext(b) Year
figure6_3a.addtext(l) rer_cpi
figure6_3a.addtext(r) a_tilde

''*******************************************************
''S1.A.Engle-Granger Approach to Cointegration
'*******************************************************
 
freeze(table_6_3_egc_rer_cpi) g1.coint(method=eg)

'The null hypothesis will be rejected as suggested by sample statistics.

''******************************************
''S1.B.Error Correction Model (ECM)
'*******************************************

''**********************************************
'Selecting the number of lags in the VAR
'***********************************************

'NOTE: We do this because we need to have the "right" number of lags when it comes time to estimate our VEC model and test for cointegration.

var var1.ls 1 4   g1
freeze(var1_lagtest1) var1.laglen(4)
freeze(var1_lagtest2) var1.testlags

'The lag length test above indicates that the VAR has 1 lag. 

var var2.ls 1 1  g1
freeze(var2_artest1) var2.correl
freeze(var2_artest2) var2.qstats(12)
freeze(var2_artest3) var2.arlm(12)

'We now try different lags of d(a_tilde), comparing SIC values across specifications.

genr resid = 0
equation eg.ls rer_cpi c a_tilde
genr ec1 = resid


var table_6_3_eg2a_cpi.ls 0 0 d(rer_cpi)   @  c ec1(-1) d(rer_cpi(-1))

var table_6_3_eg2b_cpi.ls 0 0 d(rer_cpi)   @  c ec1(-1) d(rer_cpi(-1)) d(a_tilde(-1))

var table_6_3_eg2c_cpi.ls 0 0 d(rer_cpi)   @  c ec1(-1) d(rer_cpi(-1)) d(a_tilde(-1)) d(a_tilde(-2))

'The evidence suggests that Model A is best.  Now we test that model for serial correlation.

var table_6_3_eg2a_cpi.ls 0 0 d(rer_cpi)   @   c ec1(-1) d(rer_cpi(-1)) 

freeze(table_6_3_eg2a1_cpi_artest1) table_6_3_eg2a_cpi.correl
freeze(table_6_3_eg2a2_cpi_artest2) table_6_3_eg2a_cpi.qstats(12)
freeze(table_6_3_eg2a3_cpi_artest3) table_6_3_eg2a_cpi.arlm(12)

'The residuals are not absolutely white noise. Let's try other two models.

var table_6_3_eg2b_cpi.ls 0 0 d(rer_cpi)   @   c ec1(-1) d(rer_cpi(-1)) d(a_tilde(-1))
freeze(table_6_3_eg2b1_cpi_artest1) table_6_3_eg2b_cpi.correl
freeze(table_6_3_eg2b2_cpi_artest2) table_6_3_eg2b_cpi.qstats(12)
freeze(table_6_3_eg2b3_cpi_artest3) table_6_3_eg2b_cpi.arlm(12)

'The residuals are still not white noise. 

var table_6_3_eg2c_cpi.ls 0 0 d(rer_cpi)   @   c ec1(-1) d(rer_cpi(-1)) d(a_tilde(-1)) d(a_tilde(-2))
freeze(table_6_3_eg2c1_cpi_artest1) table_6_3_eg2c_cpi.correl
freeze(table_6_3_eg2c2_cpi_artest2) table_6_3_eg2c_cpi.qstats(12)
freeze(table_6_3_eg2c3_cpi_artest3) table_6_3_eg2c_cpi.arlm(12)

'None of the above three models generate white residuals. This makes me to raise the number of lags of rer_cpi from 1 to 3. Starting with Model A, I am going to raise the number of lags of RER_CPI to 3.

var eg2d_cpi.ls 0 0 d(rer_cpi)   @   c ec1(-1) d(rer_cpi(-1)) d(rer_cpi(-2)) d(rer_cpi(-3))
freeze(table_6_3_eg2d1_cpi_artest1) eg2d_cpi.correl
freeze(table_6_3_eg2d2_cpi_artest2) eg2d_cpi.qstats(12)
freeze(table_6_3_eg2d3_cpi_artest3) eg2d_cpi.arlm(12)

'But even by adding 3 lags of rer_cpi, we do not obtain white residuals. So, let's continue with our very first model i.e. Model A and estimate EC model.

''*************************
'Estimating EC Model  
'**************************

'We'll now take the above specified model and turn it into an ECM. We shall run NW-HAC least squares model for establishing error correction mechanism.

'We now estimate the corresponding ECM:

equation table_6_3_ecm_rer_cpi.ls(n) d(rer_cpi) c ec1(-1) d(rer_cpi(-1))

'Note that the SR effect is significant as the error correction coefficient -0.39 is statistically significant at better than 1% significance level.

''**********************************************
''S2.A & S2.B: Obtaining LR Coefficients
'***********************************************

'Now, by employing FMOLS and DOLS cointegration regression estimators, finally we shall calculate our LR coefficient i.e. BS coefficient for Korea against U.S.

equation table_6_3_LReqn1a_fmols.cointreg(method=fmols) rer_cpi a_tilde

equation table_6_3_LReqn1b_dols.cointreg(method=dols, trend=constant, lag=2,lead=2 ) rer_cpi a_tilde

'The BS coefficient obtained through FMOLS and DOLS test are -0.04 and -0,05 i.e., bearing undesired sign. Thus, there is �NO� evidence in support of BS effect existing for Korea.

''******************************************
'Multivariate Cointegration Approach
'******************************************

''********************************************************
''Check if the VAR (2) model is dynamically stable
'*********************************************************
freeze(var2_varstable) var2.arroots(graph)

'The model is dynamically stable. 
''**********************************************************************
''M1.A & M1.B: Identifying the number of cointegrating vectors
'***********************************************************************

'Having identified the appropriate number of lags to put in, I now go on to test for the appropriate number of cointegrating equations.

freeze(var2_table_6_3_coint1) var2.coint(s,1)

'This command estimates all possible combinations of constants and trends in the level data series and the cointegrating equations. All the results indicate 0 cointegrating vectors.
'
'GENERAL NOTE:, in practice, cases 1 and 5 are rarely used. One should use case 1 only if one knows that all series have zero mean. Case 5 may provide a good fit in-sample but will produce implausible forecasts out-of-sample. As a rough guide, use case 2 if none of the series appear to have a trend. For trending series, use case 3 if you believe all trends are stochastic; if you believe some of the series are trend stationary, use case 4.

'Note that the 5 cases are identified under "Johansen cointegration test" in EViews. They run from most restrictive (no constants in either the level series or CEs) to most general (trend terms in both the level series and CEs).

'CONCLUSION:  I conclude that rer_cpi and a_tilde are not cointegrated in the Korean data.

'***************************************************************************************************
'CASE-2: ESTIMATING BALASSA-SAMUELSON EFFECT FOR RER_DEF & A_TILDE
'***************************************************************************************************
'*************************************************************
'STEP 0: Tests for Unit Root in Individual Time Series
'*************************************************************
'*********************************
'Graph for Korea's RER_DEF
'*********************************
                                        
genr rer_def = rer_def
freeze(figure_rer_def) rer_def.line
figure_rer_def.addtext(t) rer_def (Korea):  1970-2013
figure_rer_def.addtext(b) Year
figure_rer_def.addtext(l) rer_def
figure_rer_def.legend(off)
                                                 
'We see from the FIGURE that rer_def has time trend to it.  So we would include both an intercept and a time trend in our unit root regression equations. 

'************************************************
'ADF Unit Root Test for Korea's RER_DEF
'************************************************
 
freeze(table_6_3_rer_def_adf) rer_def.uroot(adf,trend,info=sic)

'Note that the SIC automatic lag selection picks lags, p = 1. The unit root test produces a t-value of -3.35 which is greater than our 5% criterion -3.52.  Thus, at this point, we cannot reject the null of a unit root.

'Now, let's check for white noise.  To do that, I first set all the residuals = 0, then run the ADF test and finally will check for white noise.

genr resid = 0
freeze(mode=overwrite,rer_def_adf) rer_def.uroot(adf,const,trend,info=sic)
freeze(rer_def_adf_correl) resid.correl
 
'Based on the Q-statistic, I conclude that the residuals are white noise. Putting it all together, I conclude that the rer_def series is not level stationary.

'The next thing I do is test whether the differenced series is stationary using the ADF test. I once again begin by graphing the (differenced) series.
 
genr rer_defdiff = d(rer_def)
freeze(figure_rer_defdiff) rer_defdiff.line
figure_rer_defdiff.addtext(t) drer_def (Korea):  1970-2013
figure_rer_defdiff.addtext(b) Year
figure_rer_defdiff.addtext(l) drer_def
figure_rer_defdiff.legend(off)

'From the graph, the series clearly does not have a time trend to it. So, I would test the series for unit with an intercept only.

'So we begin the whole process over again: 

genr rer_defdiff = d(rer_def)
freeze(table_6_3_rer_defdiff1_adf) rer_defdiff.uroot(adf,const,info=sic)

'Note that the SIC automatic lag selection picks no lags, p =0. The unit root test produces a t-value of -5.49 which is now smaller than our 5% criterion -2.93.  Thus, we may now reject the null of non-stationarity in first differenced series of rer_def.  There is no reason to go further.  The last thing we do is to check ADF regression result for white noise.

genr resid = 0
freeze(mode=overwrite,rer_defdiff1_adf) rer_defdiff.uroot(adf,const,info=sic)
freeze(rer_defdiff1_adf_correl) resid.correl

''Based on the Q-statistic, I conclude that the residuals are white noise.  Putting it all together, I conclude that the rer_def series is I(1).

'*****************************************************
'DF-GLS Unit Root Test for Korea's RER_DEF
'*****************************************************
 
freeze(table_6_3_rer_def_dfgls) rer_def.uroot(dfgls,trend,info=sic)

'Note that the SIC automatic lag selection picks lags, p = 1.  The unit root test produces a t-value of -3.43 which is smaller than our 5% criterion -3.19.  Thus, at this point, we may reject the null of a unit root. These findings are in contrast with ADF test results. Thus, rer_def series is I(0) according to DF-GLS test results.

'*********************************************
'Single Equation Cointegration Methods
'*********************************************

'**********************************************************
''Graph the suspected cointegrated series together
'**********************************************************

'The first step is to plot a graph of the suspected series.  This is very important!

group g2 rer_def a_tilde
freeze(figure6_3b) g2.line(x)
figure6_3b.setelem(1) lcolor(black) 
figure6_3b.setelem(2) lcolor(black) lpat(8)
figure6_3b.options linepat
figure6_3b.addtext(t) rer_def and a_tilde (Korea & U.S): 1970-2013
figure6_3b.addtext(b) Year
figure6_3b.addtext(l) rer_def
figure6_3b.addtext(r) a_tilde

''*******************************************************
''S1.A.Engle-Granger Approach to Cointegration
'*******************************************************
 
freeze(table_6_3_egc_rer_def) g2.coint(method=eg)

'The null hypothesis will be rejected as suggested by sample statistics.

''******************************************
''S1.B.Error Correction Model (ECM)
'******************************************* 
''**********************************************
'Selecting the number of lags in the VAR
'***********************************************

'NOTE: We do this because we need to have the "right" number of lags when it comes time to estimate our VEC model and test for cointegration.

var var3.ls 1 4   g2
freeze(var3_lagtest1) var3.laglen(4)
freeze(var3_lagtest2) var3.testlags

'The lag length test above indicates that the VAR has 1 lag. But the residuals are not absolutely white noise. So I am not satisfied with the selection of 1 lag. I raised the number of lags from 1 to 4 and thus obtained somewhat white residuals.

var var4.ls 1 4  g2
freeze(var4_artest1) var4.correl
freeze(var4_artest2) var4.qstats(12)
freeze(var4_artest3) var4.arlm(12)

'We now try different lags of d(a_tilde), comparing SIC values across specifications.

genr resid = 0
equation eg.ls rer_def c a_tilde
genr ec2 = resid

var table_6_3_eg2a_def.ls 0 0 d(rer_def)   @  c ec2(-1) d(rer_def(-1)) d(rer_def(-2)) d(rer_def(-3)) d(rer_def(-4))

var table_6_3_eg2b_def.ls 0 0 d(rer_def)   @  c ec2(-1) d(rer_def(-1)) d(rer_def(-2)) d(rer_def(-3)) d(rer_def(-4)) d(a_tilde(-1))

var table_6_3_eg2c_def.ls 0 0 d(rer_def)   @  c ec2(-1) d(rer_def(-1)) d(rer_def(-2)) d(rer_def(-3)) d(rer_def(-4)) d(a_tilde(-1)) d(a_tilde(-2))

'The evidence suggests that Model A is best.  Now we test that model for serial correlation.

var table_6_3_eg2a_def.ls 0 0 d(rer_def)   @   c ec2(-1) d(rer_def(-1)) d(rer_def(-2)) d(rer_def(-3)) d(rer_def(-4))
freeze(table_6_3_eg2a1_def_artest1) table_6_3_eg2a_def.correl
freeze(table_6_3_eg2a2_def_artest2) table_6_3_eg2a_def.qstats(12)
freeze(table_6_3_eg2a3_def_artest3) table_6_3_eg2a_def.arlm(12)

'The residuals are not absolutely white noise. Let's try other two models.

var table_6_3_eg2b_def.ls 0 0 d(rer_def)   @   c ec2(-1) d(rer_def(-1)) d(rer_def(-2)) d(rer_def(-3)) d(rer_def(-4)) d(a_tilde(-1))
freeze(table_6_3_eg2b1_def_artest1) table_6_3_eg2b_def.correl
freeze(table_6_3_eg2b2_def_artest2) table_6_3_eg2b_def.qstats(12)
freeze(table_6_3_eg2b3_def_artest3) table_6_3_eg2b_def.arlm(12)

'The residuals are still not white noise. 

var table_6_3_eg2c_def.ls 0 0 d(rer_def)   @   c ec2(-1) d(rer_def(-1)) d(a_tilde(-1)) d(rer_def(-2)) d(rer_def(-3)) d(rer_def(-4)) d(a_tilde(-2))
freeze(table_6_3_eg2c1_def_artest1) table_6_3_eg2c_def.correl
freeze(table_6_3_eg2c2_def_artest2) table_6_3_eg2c_def.qstats(12)
freeze(table_6_3_eg2c3_def_artest3) table_6_3_eg2c_def.arlm(12)

'None of the above three models generate white residuals. So, let's continue with our very first model i.e. Model A and estimate EC model.

''*************************
'Estimating EC Model  
'**************************

'We'll now take the above specified model and turn it into an ECM. We shall run NW-HAC least squares model for establishing error correction mechanism.

'We now estimate the corresponding ECM:

equation table_6_3_ecm_rer_def.ls(n) d(rer_def) c ec2(-1) d(rer_def(-1)) d(rer_def(-2)) d(rer_def(-3)) d(rer_def(-4))

'Note that the SR effect is significant as the error correction coefficient -0.52 is statistically significant at better than 1% significance level.

''**********************************************
''S2.A & S2.B: Obtaining LR Coefficients
'***********************************************

'Now, by employing FMOLS and DOLS cointegration regression estimators, finally we shall calculate our LR coefficient i.e. BS coefficient for Korea against U.S.

equation table_6_3_LReqn2a_fmols.cointreg(method=fmols) rer_def a_tilde

equation table_6_3_LReqn2b_dols.cointreg(method=dols, trend=constant, lag=2,lead=2 ) rer_def a_tilde

'The BS coefficients obtained through FMOLS and DOLS estimators are -0.20 and -0.21 i.e., the coefficients are bearing undesired sgns. Thus, there is 'NO' evidence in support of BS effect existing for Korea.

''******************************************
'Multivariate Cointegration Approach
'******************************************

''*************************************************************
''Check if the VAR (4) model is dynamically stable
'*************************************************************
freeze(var4_varstable) var4.arroots(graph)

'The model is dynamically stable.

''**********************************************************************
''M1.A & M1.B: Identifying the number of cointegrating vectors
'***********************************************************************

'Having identified the appropriate number of lags to put in, I now go on to test for the appropriate number of cointegrating equations.

freeze(var4_table_6_3_coint2) var4.coint(s,4)

'This command estimates all possible combinations of constants and trends in the level data series and the cointegrating equations. Trace statistic of Case 3 indicate 1 cointegrating vector.
'
'GENERAL NOTE:, in practice, cases 1 and 5 are rarely used. One should use case 1 only if one knows that all series have zero mean. Case 5 may provide a good fit in-sample but will produce implausible forecasts out-of-sample. As a rough guide, use case 2 if none of the series appear to have a trend. For trending series, use case 3 if you believe all trends are stochastic; if you believe some of the series are trend stationary, use case 4.

'Note that the 5 cases are identified under "Johansen cointegration test" in EViews. They run from most restrictive (no constants in either the level series or CEs) to most general (trend terms in both the level series and CEs).


'CONCLUSION:  I conclude that rer_def and a_tilde for Korea and U.S. are cointegrated under single equation cointegration models but are not cointegrated as proven by multivariate cointegration approach.

'*********************************************************************************************************
'CASE-3: ESTIMATING BALASSA-SAMUELSON EFFECT FOR RER_DEF_NT & A_TILDE
'*********************************************************************************************************
'*************************************************************
'STEP 0: Tests for Unit Root in Individual Time Series
'*************************************************************
'***************************************
'Graph for Korea's RER_DEF_NT
'************************************* 
genr rer_def_nt = rer_def_nt
freeze(figure_rer_def_nt) rer_def_nt.line
figure_rer_def_nt.addtext(t) rer_def_nt (Korea):  1970-2013
figure_rer_def_nt.addtext(b) Year
figure_rer_def_nt.addtext(l) rer_def_nt
figure_rer_def_nt.legend(off)
                                                 
'We see from the FIGURE that rer_def_nt has time trend to it.  So we would include both an intercept and a time trend in our unit root regression equations. 

'************************************************
'ADF Unit Root Test for Korea's RER_DEF_NT
'************************************************
 
freeze(table_6_3_rer_def_nt_adf) rer_def_nt.uroot(adf,trend,info=sic)

'Note that the SIC automatic lag selection picks lags, p = 1. The unit root test produces a t-value of -2.93 which is greater than our 5% criterion -3.52.  Thus, at this point, we cannot reject the null of a unit root.

'Now, let's check for white noise. To do that, I first set all the residuals = 0, then run the ADF test and finally will check for white noise.

genr resid = 0
freeze(mode=overwrite,rer_def_nt_adf) rer_def_nt.uroot(adf,const,trend,info=sic)
freeze(rer_def_nt_adf_correl) resid.correl
 
'Based on the Q-statistic, I conclude that the residuals are white noise.  Putting it all together, I conclude that the rer_def_nt series is not level stationary.

'The next thing I do is test whether the differenced series is stationary using the ADF test.  I once again begin by graphing the (differenced) series.
 
genr rer_def_ntdiff = d(rer_def_nt)
freeze(figure_rer_def_ntdiff) rer_def_ntdiff.line
figure_rer_def_ntdiff.addtext(t) drer_def_nt (Korea):  1970-2013
figure_rer_def_ntdiff.addtext(b) Year
figure_rer_def_ntdiff.addtext(l) drer_def_nt
figure_rer_def_ntdiff.legend(off)

'From the graph, the series clearly does not have a time trend to it. So, I would test the series for unit with an intercept only.

'So we begin the whole process over again: 

genr rer_def_ntdiff = d(rer_def_nt)
freeze(table_6_3_rer_def_ntdiff1_adf) rer_def_ntdiff.uroot(adf,const,info=sic)

'Note that the SIC automatic lag selection picks no lags, p =0.  The unit root test produces a t-value of -4.73 which is now smaller than our 5% criterion -2.93.  Thus, we may now reject the null of non-stationarity in first differenced series of rer_def_nt.  There is no reason to go further.  The last thing we do is to check ADF regression result for white noise.

genr resid = 0
freeze(mode=overwrite,rer_def_ntdiff1_adf) rer_def_ntdiff.uroot(adf,const,info=sic)
freeze(rer_def_ntdiff1_adf_correl) resid.correl

''Based on the Q-statistic, I conclude that the residuals are white noise. Putting it all together, I conclude that the rer_def_nt series is I(1).

'*****************************************************
'DF-GLS Unit Root Test for Korea's RER_DEF_NT
'*****************************************************
 
freeze(table_6_3_rer_def_nt_dfgls) rer_def_nt.uroot(dfgls,trend,info=sic)

'Note that the SIC automatic lag selection picks no lags, p = 0.  The unit root test produces a t-value of -2.97 which is greater than our 5% criterion -3.19. Thus, we may not reject the null of non-stationarity for rer_def_nt.  

'Now let's see if the series is difference stationary or not

genr rer_def_ntdiff = d(rer_def_nt)
freeze(table_6_3_rer_def_ntdiff1_dfgls) rer_def_ntdiff.uroot(dfgls,const,info=sic)

'Note that the SIC automatic lag selection picks no lags, p = 0.  The unit root test produces a t-value of -4.63 which is now smaller than our 5% criterion -1.95. Thus, we may reject the null of non-stationarity in first differenced series of a_tilde.  

''Putting it all together, I conclude that the rer_def_nt series is I(1), a finding compatible with my ADF test results.

'*********************************************
'Single Equation Cointegration Methods
'*********************************************

'**********************************************************
''Graph the suspected cointegrated series together
'**********************************************************

'The first step is to plot a graph of the suspected series.  This is very important!

group g3 rer_def_nt a_tilde
freeze(figure6_3) g3.line(x)
figure6_3.setelem(1) lcolor(black) 
figure6_3.setelem(2) lcolor(black) lpat(8)
figure6_3.options linepat
figure6_3.addtext(t) rer_def_nt and a_tilde (Korea & U.S): 1970-2013
figure6_3.addtext(b) Year
figure6_3.addtext(l) rer_def_nt
figure6_3.addtext(r) a_tilde

''*******************************************************
''S1.A.Engle-Granger Approach to Cointegration
'*******************************************************

freeze(table_6_3_egc_rer_def_nt) g3.coint(method=eg)

'The null hypothesis will not be rejected as suggested by sample statistics.

''******************************************
''S1.B.Error Correction Model (ECM)

''******************************************
''**********************************************
'Selecting the number of lags in the VAR
'***********************************************

'NOTE: We do this because we need to have the "right" number of lags when it comes time to estimate our VEC model and test for cointegration.

var var5.ls 1 4   g3
freeze(var5_lagtest1) var5.laglen(4)
freeze(var5_lagtest2) var5.testlags

'The lag length test above indicates that the VAR has 1 lags. But the residuals are not absolutely white noise. So I am not satisfied with the selection of 1 lag. I raised the number of lags from 1 to 4 and thus obtained somewhat white residuals.

var var6.ls 1 4  g3
freeze(var6_artest1) var6.correl
freeze(var6_artest2) var6.qstats(12)
freeze(var6_artest3) var6.arlm(12)

'We now try different lags of d(a_tilde), comparing SIC values across specifications.

genr resid = 0
equation eg.ls rer_def_nt c a_tilde
genr ec3 = resid

var table_6_3_eg2a_def_nt.ls 0 0 d(rer_def_nt)   @  c ec3(-1) d(rer_def_nt(-1)) d(rer_def_nt(-2)) d(rer_def_nt(-3)) d(rer_def_nt(-4))

var table_6_3_eg2b_def_nt.ls 0 0 d(rer_def_nt)   @  c ec3(-1) d(rer_def_nt(-1)) d(rer_def_nt(-2)) d(rer_def_nt(-3)) d(rer_def_nt(-4)) d(a_tilde(-1))

var table_6_3_eg2c_def_nt.ls 0 0 d(rer_def_nt)   @  c ec3(-1) d(rer_def_nt(-1)) d(rer_def_nt(-2)) d(rer_def_nt(-3)) d(rer_def_nt(-4)) d(a_tilde(-1)) d(a_tilde(-2))

'The evidence suggests that Model A is best.  Now we test that model for serial correlation.

var table_6_3_eg2a_def_nt.ls 0 0 d(rer_def_nt)   @   c ec3(-1) d(rer_def_nt(-1)) d(rer_def_nt(-2)) d(rer_def_nt(-3)) d(rer_def_nt(-4))
freeze(table_6_3_eg2a1_def_nt_artest1) table_6_3_eg2a_def_nt.correl
freeze(table_6_3_eg2a2_def_nt_artest2) table_6_3_eg2a_def_nt.qstats(12)
freeze(table_6_3_eg2a3_def_nt_artest3) table_6_3_eg2a_def_nt.arlm(12)

'The residuals are not absolutely white noise. Let's try other two models.

var table_6_3_eg2b_def_nt.ls 0 0 d(rer_def_nt)   @   c ec3(-1) d(rer_def_nt(-1)) d(rer_def_nt(-2)) d(rer_def_nt(-3)) d(rer_def_nt(-4)) d(a_tilde(-1))
freeze(table_6_3_eg2b1_def_nt_artest1) table_6_3_eg2b_def_nt.correl
freeze(table_6_3_eg2b2_def_nt_artest2) table_6_3_eg2b_def_nt.qstats(12)
freeze(table_6_3_eg2b3_def_nt_artest3) table_6_3_eg2b_def_nt.arlm(12)

'The residuals are still not white noise. 

var table_6_3_eg2c_def_nt.ls 0 0 d(rer_def_nt)   @   c ec3(-1) d(rer_def_nt(-1)) d(rer_def_nt(-2)) d(rer_def_nt(-3)) d(rer_def_nt(-4)) d(a_tilde(-1)) d(a_tilde(-2))
freeze(table_6_3_eg2c1_def_nt_artest1) table_6_3_eg2c_def_nt.correl
freeze(table_6_3_eg2c2_def_nt_artest2) table_6_3_eg2c_def_nt.qstats(12)
freeze(table_6_3_eg2c3_def_nt_artest3) table_6_3_eg2c_def_nt.arlm(12)

'None of the above three models generate white residuals. So, let's continue with our very first model i.e. Model A and estimate EC model.

''*************************
'Estimating EC Model  
'**************************

'We'll now take the above specified model and turn it into an ECM. We shall run NW-HAC least squares model for establishing error correction mechanism.

'We now estimate the corresponding ECM:

equation table_6_3_ecm_rer_def_nt.ls(n) d(rer_def_nt) c ec3(-1) d(rer_def_nt(-1)) d(rer_def_nt(-2)) d(rer_def_nt(-3)) d(rer_def_nt(-4))

'Note that the SR effect is significant as the error correction coefficient -0.43 is statistically significant at better than 1% significance level.

''**********************************************
''S2.A & S2.B: Obtaining LR Coefficients
'***********************************************

'Now, by employing FMOLS and DOLS cointegration regression estimators, finally we shall calculate our LR coefficient i.e. BS coefficient for Korea against U.S.

equation table_6_3_LReqn3a_fmols.cointreg(method=fmols) rer_def_nt a_tilde

equation table_6_3_LReqn3b_dols.cointreg(method=dols, trend=constant, lag=2,lead=2 ) rer_def_nt a_tilde

'The BS coefficients obtained through FMOLS and DOLS estimators are 0.02 and 0.01 and are statistically insignificant. Thus, there is 'NO' evidence in support of BS effect existing for Korea.

''******************************************
'Multivariate Cointegration Approach
'******************************************

''********************************************************
''Check if the VAR (6) model is dynamically stable
'*********************************************************
freeze(var6_varstable) var6.arroots(graph)

'The model is dynamically stable.

''**********************************************************************
''M1.A & M1.B: Identifying the number of cointegrating vectors
'***********************************************************************

'Having identified the appropriate number of lags to put in, I now go on to test for the appropriate number of cointegrating equations.

freeze(var6_table_6_3_coint3) var6.coint(s,4)

'This command estimates all possible combinations of constants and trends in the level data series and the cointegrating equations. Trace statistic of Case 3 indicate 1 cointegrating vector.
'
'GENERAL NOTE:, in practice, cases 1 and 5 are rarely used. One should use case 1 only if one knows that all series have zero mean. Case 5 may provide a good fit in-sample but will produce implausible forecasts out-of-sample. As a rough guide, use case 2 if none of the series appear to have a trend. For trending series, use case 3 if you believe all trends are stochastic; if you believe some of the series are trend stationary, use case 4.

'Note that the 5 cases are identified under "Johansen cointegration test" in EViews. They run from most restrictive (no constants in either the level series or CEs) to most general (trend terms in both the level series and CEs).

''******************************************************************
''M2.A, M2.B & M3: Vector Error Correction Model (VECM)
'*******************************************************************

' For estimating the LR relationship, corresponding VEC command is:

var table_6_3_vec3d.ec(d,1) 1 3 rer_def_nt a_tilde

'CONCLUSION:  I conclude that rer_def_nt and a_tilde are not cointegrated in the Korea's data.


